import { ref } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/store'
// import { login } from '@/api/api'

// import Layout from '@/views/basic/Layout.vue'
// import Home from '@/views/home/index.vue'
// import AccountList from '@/views/account/index.vue'
// import AccountEdit from '@/views/account/Edit.vue'
// import PowerList from '@/views/power/index.vue'
// import PageList from '@/views/pageSetting/index.vue'

// const pages = {
//     'Layout': () => import(`@/views/basic/Layout.vue`),
//     'Home': () => import(`@/views/home/index.vue`),
//     'AccountList': () => import(`@/views/account/index.vue`),
//     'AccountEdit': () => import(`@/views/account/Edit.vue`),
//     'PowerList': () => import(`@/views/power/index.vue`),
//     'PageList': () => import(`@/views/pageSetting/index.vue`)
// }

// const asyncRoutes = ref([])
// const res: any = await login()
// asyncRoutes.value = res.result.routes

export let routes = [
    {
        path: "/",
        name: 'login',
        component: () => import(`@/views/login/index.vue`),
        meta: {
            isMenu: false,
            isRoute: true,
            title: '登录',
            hasSubMenu: false,
            activeMenu: '/'
        }
    },
    {
        path: "/index",
        name: 'index',
        component: () => import(`@/views/basic/Layout.vue`),
        redirect: '/home',
        meta: {
            isMenu: true,
            isRoute: true,
            title: '首页',
            hasSubMenu: false,
            icon: 'house',
        },
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import(`@/views/home/index.vue`),
                meta: {
                    isMenu: false,
                    isRoute: true,
                    title: '首页',
                    hasSubMenu: false,
                    activeMenu: '/index'
                }
            },
        ]
    },
    {
        path: "/user",
        name: 'user',
        component: () => import(`@/views/basic/Layout.vue`),
        meta: {
            isMenu: true,
            isRoute: false,
            title: '用户管理',
            hasSubMenu: true,
            icon: 'user-filled'
        },
        children: [
            {
                path: "account",
                name: 'account',
                component: () => import(`@/views/account/index.vue`),
                meta: {
                    isMenu: true,
                    isRoute: true,
                    title: '账号管理',
                    hasSubMenu: false,
                    activeMenu: '/user/account'
                },
            },
            {
                path: "account/edit/:id?",
                name: 'accountEdit',
                component: () => import(`@/views/account/Edit.vue`),
                meta: {
                    isMenu: false,
                    isRoute: true,
                    title: '账号编辑',
                    hasSubMenu: false,
                    activeMenu: '/user/account'
                }
            },
            {
                path: "power",
                name: 'power',
                component: () => import(`@/views/power/index.vue`),
                meta: {
                    isMenu: true,
                    isRoute: true,
                    title: '角色权限',
                    hasSubMenu: false,
                    activeMenu: '/user/power'
                }
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 大菠萝这个状态管理器就这一点不好，有问题，官网都有这个问题
setTimeout(() => {
    let User = useUserStore()
    // let allRoutes = routes.concat(asyncRoutes.value)
    User.setAllRoutes(routes)
}, 0)

export default router